<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/7/16
 * Time: 16:11
 * Created by Created by Panxsoft.
 */

namespace App\Services\Youzan;

use App\Models\Youzan\YouzanAuthorizer;

class OrderService
{
    /**
     * 有赞订单 buyer_messages 字段中能判断为魔妆镜订单的字符串
     */
    const YOUZAN_PRODUCT_BELONG_TO = 'product_belong_to';

    /**
     *
     * @link https://open.youzan.com/apilist/detail/group_buyer/buyer_bill/youzan.trade.bill.good.url.get
     * @param YouzanAuthorizer $youzanAuthorizer
     * @param array $params
     * @return array
     * @throws \Exception
     * @throws \Throwable
     */
    public function createOrder(YouzanAuthorizer $youzanAuthorizer, array $params)
    {
        /**
         * @see Mock::createOrder()
         */
        $create = (new OrderRepository())->createTradeBillGood($youzanAuthorizer, $params);

        return $create;
    }

    /**
     * @param array $order_list
     *
     * @see Mock::queryOrders()
     * @see Mock::transformOrderLists()
     *
     * @return array
     */
    public function transformOrderLists(array $order_list): array
    {
        $data = [];
        foreach ($order_list as $order) {
            $buyer_msg_str = $order['full_order_info']['orders'][0]['buyer_messages'];
            if (str_contains($buyer_msg_str, self::YOUZAN_PRODUCT_BELONG_TO)) {
                $data []= $this->assembleOrderData($buyer_msg_str, $order);
            };
        }

        return $data;
    }

    /**
     * @param $buyer_msg_str
     * @param array $order
     *
     * @return array
     */
    private function assembleOrderData($buyer_msg_str, array $order): array
    {
        $buyer_messages = json_decode($buyer_msg_str);
        $buyer_msg = json_decode($buyer_messages[0]);

        $order_info = $order['full_order_info']['order_info'];
        $pay_info = $order['full_order_info']['pay_info'];
        $buyer_info = $order['full_order_info']['buyer_info'];
        $order_item = $order['full_order_info']['orders'];

        $data = [
            'customer_id'       => $buyer_msg->customer_id,
            'device_account_id' => $buyer_msg->device_account_id,
            'store_id'          => $buyer_msg->store_id,
            'product_id'        => $buyer_msg->product_id ?? 0,
            'trad_no'           => $order_info['tid'],
            'created_at'        => $order_info['created'],
            'updated_at'        => $order_info['update_time'],
            'status_str'        => $order_info['status_str'],
            'pay_time'          => $order_info['pay_time'],
            'total_fee'         => $pay_info['payment'], // total_fee + post_fee
            'buyer'             => $buyer_info['fans_nickname'],
            'refund_state'      => $order_info['refund_state'],
            'num'               => $order_item[0]['num'],
            'price'             => $order_item[0]['price'],
            'product_json_data' => [
                'product_thumb_url' => $buyer_msg->product_thumb_url ?? null,
                'product_title'     => $buyer_msg->product_title ?? null,
            ],
            'product_belong_to' => $buyer_msg->product_belong_to ?? null,
        ];

        return $data;
    }
}